package com.core.admin.action.service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.common.entity.WebSite;
import com.common.web.ContextPvd;
import com.common.web.MemberUtils;
import com.common.web.ResponseUtils;
import com.common.web.impl.ContextPvdImpl;
import com.core.entity.User;
/**
 * 配置站点全局参数信息
 * @author hp
 *
 */
public class WebSiteAdminInterceptor extends HandlerInterceptorAdapter{

	@Resource(type=ContextPvdImpl.class)
	ContextPvd contextPvd;
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		/*
		 * 获取网络链接地址 如本地：http://127.0.0.1/[port]/[project]
		 */
		String ctx=contextPvd.getNetWorkPath();
		request.setAttribute("ctx", ctx);
		//获取请求路径 如：/index.do
		String path = request.getPathInfo();
		//获取请求参数：如：a=1&b=2
		String queryString = request.getQueryString();
		WebSite site=new WebSite();
		site.setBeforePath(path, queryString);
		request.setAttribute("beforePath", site.BEFORE_PATH);
		super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String path = request.getPathInfo();
		if(!path.startsWith("/login")&&!path.startsWith("/validateLogin")){
			User user=MemberUtils.getLoginMember();
	        if(user==null){
	        	ResponseUtils.renderJsRedirect(response, "login.do");
	        	return false;
	        }
		}
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		return super.preHandle(request, response, handler);
	}

}
